package com.obsidian.free.slideshow;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class SlideshowLarge extends AppWidgetProvider {
   @Override
   public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
      if(appWidgetIds == null) {
         appWidgetIds = appWidgetManager.getAppWidgetIds(
               new ComponentName(context, SlideshowLarge.class));
      }
      
      for(int appWidgetId : appWidgetIds) {
         Intent intent = new Intent(context, SlideshowUpdateService.class);
         intent.putExtra("size", SlideshowUpdateService.SIZE_LARGE);
         intent.putExtra("appWidgetId", appWidgetId);
         context.startService(intent);
      }
   }
   
   @Override
   public void onReceive(Context context, Intent intent) {
      Log.d("Slideshow", intent.getAction());
      if(intent.getAction().equals("android.appwidget.action.APPWIDGET_DELETED")) {
         ImageDatabase db = new ImageDatabase(context);
         int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                                              AppWidgetManager.INVALID_APPWIDGET_ID);
         db.deleteWidget(appWidgetId);
         db.close();
         onDeleted(context, new int[] {appWidgetId});
      }
      super.onReceive(context, intent);
   }
}
